package com.tianyang.yukuaifu.business.trade.presenter;

import com.tianyang.yukuaifu.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifu.business.trade.contract.BankManageContract;
import com.tianyang.yukuaifu.business.trade.model.TradeModel;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：银行卡管理
 * Created by xyj on 2024/3/10
 */
public class BankManagePresenter implements BankManageContract.Presenter {

    private BankManageContract.View mView;

    private CompositeSubscription mSubscription;

    private boolean isAgent;

    public BankManagePresenter(BankManageContract.View mView, boolean isAgent) {
        this.mView = mView;
        this.isAgent = isAgent;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void queryUserSettlementCard() {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance().queryUserSettlementCard(isAgent)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettleBankOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onNext(SettleBankOutput output) {
                        mView.showSettlementCardView(output, isAgent);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
